package com.woniuxy.background.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.background.advice.SysLog;
import com.woniuxy.background.common.RestResult;
import com.woniuxy.background.pojo.Goods;
import com.woniuxy.background.pojo.GoodsType;
import com.woniuxy.background.service.GoodsService;
import com.woniuxy.background.service.GoodsTypeService;
import com.woniuxy.background.vo.GoodVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsTypeService goodsTypeService;


    @RequestMapping("/list")
    @ResponseBody
    @RequiresPermissions("good:good:list")
    public RestResult list(@RequestParam(value = "page",defaultValue = "1",required = false) Integer currentPage,
                           @RequestParam(value = "limit",defaultValue = "5",required = false) Integer pageSize,
                           GoodVO vo){
        PageInfo pageInfo = goodsService.queryGoodsByPage(currentPage,pageSize,vo);
        return RestResult.success(pageInfo);
    }

    @RequestMapping("/typeList")
    @ResponseBody
    @RequiresPermissions("good:gt:list")
    public RestResult typeList(){
        List<GoodsType> goodsTypes = goodsTypeService.selectAll();
        return RestResult.success(goodsTypes);
    }

    @DeleteMapping("/deleteType/{gtid}")
    @ResponseBody
    @SysLog(value = "删除拍品分类")
    @RequiresPermissions("good:gt:delete")
    public RestResult deleteGoodType(@PathVariable("gtid")int gtid){
        try {
            goodsTypeService.deleteByPrimaryKey(gtid);
        }catch (Exception e){
            e.printStackTrace();
            return RestResult.failed("删除失败！");
        }
        return RestResult.success("删除成功！");
    }
    @PostMapping("/saveGoodType")
    @ResponseBody
    @SysLog(value = "添加分类")
    @RequiresPermissions("good:gt:save")
    public RestResult saveGoodType(GoodsType goodsType){
        try {
            goodsTypeService.insertSelective(goodsType);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.failed("添加失败");
        }
        return RestResult.success("添加成功");
    }

    @RequestMapping("/addGoodType")
    public ModelAndView addGT(ModelAndView modelAndView){
        modelAndView.setViewName("good/addGoodType");
        return modelAndView;
    }

    @DeleteMapping("/delete/{gid}")
    @ResponseBody
    @SysLog(value = "删除拍品")
    @RequiresPermissions("good:good:delete")
    public RestResult deleteGood(@PathVariable("gid")int gid){
        try {
            goodsService.deleteByPrimaryKey(gid);
        }catch (Exception e){
            e.printStackTrace();
            return RestResult.failed("删除失败！");
        }
        return RestResult.success("删除成功！");
    }

    @PostMapping("/updGood")
    @ResponseBody
    @SysLog(value = "更新拍品")
    @RequiresPermissions("good:good:update")
    public RestResult updUser(Goods goods){
        try {
            goodsService.updateByPrimaryKeySelective(goods);
        }catch (Exception e){
            e.printStackTrace();
            RestResult.failed("更新失败！");
        }
        return RestResult.success("更新成功！");
    }

    @GetMapping("/goodEdit/{gid}")
    public ModelAndView userEdit(@PathVariable("gid")int gid, ModelAndView modelAndView){
        List<GoodsType> goodsTypes = goodsTypeService.selectAll();
        modelAndView.addObject("goodsTypes",goodsTypes);
        Goods goods = goodsService.selectByPrimaryKey(gid);
        modelAndView.addObject("goods",goods);
        modelAndView.setViewName("good/editGood");
        return modelAndView;
    }
}
